package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.constants.MqConstants;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.service.IMqPushService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MqPushServiceImpl implements IMqPushService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 自动报名智能课或刷题班
     *
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param subjectsId
     * @param lessonType
     * @param studentId
     */
    @Override
    public boolean autoOrderApply(Integer yearPart, Integer quarterNum, Long schoolId, Long subjectsId, Integer lessonType, Long studentId) {
        try {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setYearPart(yearPart);
            orderDetail.setQuarterNum(quarterNum);
            orderDetail.setSchoolId(schoolId);
            orderDetail.setSubjectsId(subjectsId);
            orderDetail.setLessonType(lessonType);
            orderDetail.setStudentId(studentId);
            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_AUTO_ORDER_APPLY_FANOUT, null, orderDetail);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean updateResidueCourse(Integer yearPart, Integer quarterNum, Long schoolId, Long subjectsId, Integer lessonType, Long studentId) {
        try {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setYearPart(yearPart);
            orderDetail.setQuarterNum(quarterNum);
            orderDetail.setSchoolId(schoolId);
            orderDetail.setSubjectsId(subjectsId);
            orderDetail.setLessonType(lessonType);
            orderDetail.setStudentId(studentId);
            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_UPDATE_RESIDUE_COURSE_FANOUT, null, orderDetail);
        } catch (Exception e) {
            return false;
        }
        return true;
    }


    @Override
    public boolean changeSchoolUpdateFaceData(ChangeSchoolDetail changeSchoolDetail) {
        try {
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_CHANGE_SCHOOL_UPDATE_FACE_FANOUT, null, changeSchoolDetail);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean pushWechart(String content){
        try {
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_WECHAR_PUSH_FANOUT, null, content);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    /**
     * 发送多个时间的信息
     * @param multiDelayMessage
     */
    @Override
    public void classUpRemindMulti(MultiDelayMessage<T> multiDelayMessage){
        try{
            //MultiDelayMessage<MqSchedulingLog> msg = MultiDelayMessage.of(mqSchedulingLog,10000L,30000L);
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_DELAY_CLASS_REMIND_DIRECT, null, multiDelayMessage);
        }catch (Exception e){
        }
    }

    @Override
    public void classUpRemind(MqSchedulingLog mqSchedulingLog){
//        try{
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_DELAY_CLASS_REMIND_DIRECT, null, mqSchedulingLog, new MessagePostProcessor() {
//                @Override
//                public Message postProcessMessage(Message message) throws AmqpException {
//                    // 添加延迟消息属性
//                    Integer delayNum = 1000 * Integer.parseInt(mqSchedulingLog.getDelayedSecond().toString());
//                    message.getMessageProperties().setDelay(delayNum);
//                    return message;
//                }
//            });
//        }catch (Exception e){
//        }
    }


    /**
     * 更新分班班信学生最后一条标记
     * @param yearPart
     * @param quarterNum
     * @param studentId
     * @param subjectsId
     * @param lessonType
     */
    @Override
    public void updateStudentGradeLast(Integer yearPart, Integer quarterNum, Long studentId, Long subjectsId, Integer lessonType){
        try{
            StudentGrade studentGrade = new StudentGrade();
            studentGrade.setYearPart(yearPart);
            studentGrade.setQuarterNum(quarterNum);
            studentGrade.setStudentId(studentId);
            studentGrade.setSubjectsId(subjectsId);
            studentGrade.setLessonType(lessonType);
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_STUDENT_GRADE_LAST_FANOUT, null, studentGrade);
        }catch (Exception e){
        }
    }

    /**
     * 添加缺课数据
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param studentId
     * @param subjectsId
     * @param yearClassId
     * @param gradeId
     * @param type
     */
    @Override
    public void lackCourseLogBatchAdd(Integer yearPart, Integer quarterNum, Long schoolId, Long studentId, Long subjectsId, Integer yearClassId, Long gradeId, Integer type){
        try {
            LackCourseLog lackCourseLog = new LackCourseLog();
            lackCourseLog.setYearPart(yearPart);
            lackCourseLog.setQuarterNum(quarterNum);
            lackCourseLog.setSchoolId(schoolId);
            lackCourseLog.setStudentId(studentId);
            lackCourseLog.setSubjectsId(subjectsId);
            lackCourseLog.setYearClassId(yearClassId);
            lackCourseLog.setGradeId(gradeId);
            lackCourseLog.setType(type);
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_ADD_LACK_COURSE_lOG_FANOUT, null, lackCourseLog);
        }catch (Exception e){
        }
    }

    /**
     * 添加学生课次
     * @param gradeId
     * @param studentId
     */
    @Override
    public void addStudentLesson(Long gradeId, Long studentId){
        try{
            StudentLessonNum studentLessonNum = new StudentLessonNum();
            studentLessonNum.setGradeId(gradeId);
            studentLessonNum.setStudentId(studentId);
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_ADD_STUDENT_LESSON_FANOUT, null, studentLessonNum);

        }catch (Exception e){

        }
    }

    /**
     * 推送数据到mq，添加学生帐号
     * @param studentId
     * @param subjectsId
     */
    @Override
    public void updateStudentAccount(Integer yeartPart,Integer quarterNum,Long studentId, Long subjectsId){
        try{
            //先发送到erp，Mq添加好帐号，再从erpMq分发给其它系统
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_ADD_STUDENT_ACCOUNT_FANOUT, null, yeartPart+"-"+quarterNum+"-"+studentId+"-"+subjectsId);
        }catch (Exception e){
            System.out.println("out:"+e.getMessage());
        }
    }

}
